package com.ibeeking.found.report.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ibeeking.found.report.rest.service.IReportDashboardService;
import com.ibeeking.found.report.rest.service.IReportDashboardWidgetService;
import com.ibeeking.found.report.rest.service.IReportExcelService;
import com.ibeeking.found.report.rest.service.IReportService;
import com.ibeeking.found.report.service.common.constant.enums.BaseOperationEnum;
import com.ibeeking.found.report.service.common.constant.enums.ReportTypeEnum;
import com.ibeeking.found.report.service.common.dos.ReportDO;
import com.ibeeking.found.report.service.common.dos.ReportDashboardDO;
import com.ibeeking.found.report.service.common.dos.ReportDashboardWidgetDO;
import com.ibeeking.found.report.service.common.dos.ReportExcelDO;
import com.ibeeking.found.report.service.common.vo.ReportVO;
import com.ibeeking.found.report.service.mapper.ReportBaseMapper;
import com.ibeeking.found.report.service.mapper.ReportMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author chenkening
 * @date 2021/3/26 10:35
 */
@Service
public class ReportServiceImpl implements IReportService {

    @Resource
    private ReportMapper reportMapper;
    @Resource
    private IReportDashboardService reportDashboardService;
    @Resource
    private IReportDashboardWidgetService reportDashboardWidgetService;
    @Resource
    private IReportExcelService reportExcelService;

    @Override
    public ReportBaseMapper<ReportDO> getMapper() {
        return reportMapper;
    }


    @Override
    public void processBatchBeforeOperation(List<ReportDO> entities, BaseOperationEnum operationEnum) throws BusinessException {
        IReportService.super.processBatchAfterOperation(entities, operationEnum);
        switch (operationEnum) {
            case DELETE_BATCH:
                entities.forEach(report -> {
                    Long id = report.getId();
                    ReportDO delReportDO = selectOne(id);
                    if (null == delReportDO) {
                        return;
                    }
                    String reportCode = delReportDO.getReportCode();
                    String reportType = delReportDO.getReportType();
                    switch (ReportTypeEnum.valueOf(reportType)) {
                        case report_screen:
                            LambdaQueryWrapper<ReportDashboardDO> reportDashboardLambdaQueryWrapper = Wrappers.lambdaQuery();
                            reportDashboardLambdaQueryWrapper.eq(ReportDashboardDO::getReportCode, reportCode);
                            reportDashboardService.delete(reportDashboardLambdaQueryWrapper);

                            LambdaQueryWrapper<ReportDashboardWidgetDO> reportDashboardWidgetLambdaQueryWrapper = Wrappers.lambdaQuery();
                            reportDashboardWidgetLambdaQueryWrapper.eq(ReportDashboardWidgetDO::getReportCode, reportCode);
                            reportDashboardWidgetService.delete(reportDashboardWidgetLambdaQueryWrapper);

                            break;
                        case report_excel:
                            LambdaQueryWrapper<ReportExcelDO> reportExcelLambdaQueryWrapper = Wrappers.lambdaQuery();
                            reportExcelLambdaQueryWrapper.eq(ReportExcelDO::getReportCode, reportCode);
                            reportExcelService.delete(reportExcelLambdaQueryWrapper);
                            break;
                        default:
                    }
                });
                break;
            default:

        }
    }

    /**
     * 下载次数+1
     *
     * @param reportCode
     */
    @Override
    public void downloadStatistics(String reportCode) {
        ReportDO reportDO = selectOne("report_code", reportCode);
        if (null != reportDO) {
            Long downloadCount = reportDO.getDownloadCount();
            if (null == downloadCount) {
                downloadCount = 0L;
            }else {
                downloadCount++;
            }
            reportDO.setDownloadCount(downloadCount);
            update(reportDO);
        }

    }

    @Override
    public void copy(ReportVO dto) {
        if (null == dto.getId()) {
            throw new BusinessException("id不能为空");
        }
        if (StringUtils.isBlank(dto.getReportCode())) {
            throw new BusinessException("报表编码不能为空");
        }
        ReportDO reportDO = selectOne(dto.getId());
        String reportCode = reportDO.getReportCode();
        ReportDO copyReportDO = copyReport(reportDO, dto);
        //复制主表数据
        insert(copyReportDO);
        String copyReportCode = copyReportDO.getReportCode();
        String reportType = reportDO.getReportType();
        switch (ReportTypeEnum.valueOf(reportType)) {
            case report_screen:
                //查询看板
                ReportDashboardDO reportDashboardDO = reportDashboardService.selectOne("report_code", reportCode);
                if (null != reportDashboardDO) {
                    reportDashboardDO.setId(null);
                    reportDashboardDO.setReportCode(copyReportCode);
                    reportDashboardService.insert(reportDashboardDO);
                }

                //查询组件
                List<ReportDashboardWidgetDO> reportDashboardWidgetDOList = reportDashboardWidgetService.list("report_code", reportCode);
                if (!CollectionUtils.isEmpty(reportDashboardWidgetDOList)) {
                    String finalCopyReportCode = copyReportCode;
                    reportDashboardWidgetDOList.forEach(reportDashboardWidget -> {
                        reportDashboardWidget.setId(null);
                        reportDashboardWidget.setReportCode(finalCopyReportCode);
                    });
                    reportDashboardWidgetService.insertBatch(reportDashboardWidgetDOList);
                }

                break;
            case report_excel:
                ReportExcelDO reportExcelDO = reportExcelService.selectOne("report_code", reportCode);
                if (null != reportExcelDO) {
                    reportExcelDO.setId(null);
                    reportExcelDO.setReportCode(copyReportCode);
                    reportExcelService.insert(reportExcelDO);
                }

                break;
            default:
        }
    }

    private ReportDO copyReport(ReportDO reportDO, ReportVO dto){
        //复制主表数据
        ReportDO copyReportDO = BeanUtil.convertBean(reportDO, ReportDO.class);
//        ReportBeanUtils.copyAndFormatter(reportDO, copyReportDO);
        copyReportDO.setReportCode(dto.getReportCode());
        copyReportDO.setReportName(dto.getReportName());
        return copyReportDO;
    }

    @Override
    public void processBeforeOperation(ReportDO entity, BaseOperationEnum operationEnum) throws BusinessException {

    }
}
